package com.ysx.attendance.service.impl;

import com.ysx.attendance.bean.ClassBean;
import com.ysx.attendance.bean.DelayBean;
import com.ysx.attendance.bean.StudentBean;
import com.ysx.attendance.bean.TeacherBean;
import com.ysx.attendance.dao.*;
import com.ysx.attendance.dto.CakeDTO;
import com.ysx.attendance.entity.ClassEntity;
import com.ysx.attendance.service.ClassService;
import com.ysx.utils.DaoFactory;

import java.util.ArrayList;
import java.util.List;

public class ClassServiceImpl  implements ClassService {


    private ClassDao classDao=null;
    private AttendanceDao attendanceDao=null;
    private UserRoleDao userRoleDao=null;
    private RoleDao roleDao=null;
    private PermissionDao permissionDao=null;
    private ClassTeacherStudentDao classTeacherStudentDao=null;

    {
        DaoFactory<ClassDao> classDaoFactory = new DaoFactory();
        classDao =classDaoFactory.getDao(ClassDao.class);

        DaoFactory<AttendanceDao> attendanceDaoFactory = new DaoFactory();
        attendanceDao =attendanceDaoFactory.getDao(AttendanceDao.class);

        DaoFactory<UserRoleDao> userRoleDaoFactory = new DaoFactory();
        userRoleDao =userRoleDaoFactory.getDao(UserRoleDao.class);

        DaoFactory<RoleDao> roleDaoFactory = new DaoFactory();
        roleDao =roleDaoFactory.getDao(RoleDao.class);

        DaoFactory<PermissionDao> permissionDaoFactory = new DaoFactory();
        permissionDao =permissionDaoFactory.getDao(PermissionDao.class);

        DaoFactory<ClassTeacherStudentDao> classTeacherStudentDaoFactory = new DaoFactory();
        classTeacherStudentDao =classTeacherStudentDaoFactory.getDao(ClassTeacherStudentDao.class);
    }


    @Override
    public int deleteByIds(List<Integer> ids){
        return attendanceDao.deleteByIds(ids);
    }
    @Override
    public List<DelayBean> getDelayList() {
        return attendanceDao.getDelayList();
    }


    /**
     * 查所有的班级
     * @return
     */
    @Override
    public List<ClassBean> findClassList(Integer userId) {

        List<ClassBean> rs = new ArrayList<>();
        // 1 根据user_id=10010查找这个用户的角色s

        List<Integer> roleIdList = userRoleDao.findRoleIdListByUserId(userId);

        for(Integer roleId:roleIdList){

            // 2 根据ROLE_ID=800300 去角色表查她的角色名称

            String roleName = roleDao.findRoleNameById(roleId);

            // 2.5 根据角色名称去 权限表中去查 看这个角色有哪些资源
            // pageList ['/attendance/add.html','/class/report.html']
            List<String> pageList = permissionDao.findPageListByRoleName(roleName);

            // page '/attendance/add.html'  'add.html'
            for(String page:pageList){

                if(null==page || "".equals(page)){
                    return rs;
                }

                if(page.contains("add.html")){
                    // 说明可以去 考勤新增页面

                    // 修改以前的逻辑： 根据角色名称 决定可以 看到多少数据

                    // 学生 不给你看数据   班级考勤管理员 1个  校级考勤管理员 看所有班

                    if("学生".equals(roleName)){
                        return rs;
                    }else if("班级考勤管理员".equals(roleName)) {
                        //看1班

                        // 已知 如果当前角色是 班级考勤管理员 就看你是哪1个班 classId
                        Integer classId = classTeacherStudentDao.findClassIdByUserId(userId);


                        ClassBean clzBean= findTeacherAndStudentListByClassId(classId);
                        rs.add(clzBean);
                        continue;


                    }else if("校级考勤管理员".equals(roleName)){
                        //看多班
                        List<ClassBean> classList = classDao.findClassList();

                        // 如果当前角色是 校级考勤管理员 就看你是几个班 classId
                        for(ClassBean classBean: classList){

                            ClassBean clzBean= findTeacherAndStudentListByClassId(classBean.getClassId());
                            rs.add(clzBean);
                        }
                    }



                   /* // 封装页面的数据逻辑 BEGIN
                    List<ClassBean> classList = classDao.findClassList();

                    for(ClassBean classBean: classList){

                        ClassBean clzBean= findTeacherAndStudentListByClassId(classBean.getClassId());
                        rs.add(clzBean);
                    }
                    // 封装页面的数据逻辑 END*/
                }
            }
        }


        return rs;
    }

    @Override
    public List<CakeDTO> getCake(Integer userId) {

        // 1 根据用户id找到这个班级管理员所在地班级
        Integer classId= classTeacherStudentDao.findClassIdByUserId(userId);
        List<CakeDTO> cakeList = attendanceDao.getCake(classId);
        return cakeList;
    }



    @Override
    public int saveData(String[] ids, String delayDate) {

        DaoFactory<AttendanceDao> daoFactory = new DaoFactory();

        //会自动提交事务的DAO 因为在新增 修改 删除操作中 事务只有提交才会真正保存到表中
        attendanceDao =daoFactory.getDaoTransaction(AttendanceDao.class);

        //1 循环ids 拿到1个id  如果拿到的ids=[10010 10020] 我保存2次  OK
        try{
            for(String id:ids){
                int userId= Integer.parseInt(id);
                // 调用dao的方法保存到数据库中
                // 方案1: 先循环 后单个新增  优点：降低了开发的难度 缺点：多次连接数据库 时间成本很高
                // 作业：方案2: 批量新增 一次就把数据写到表中  优点：1次连接 缺点：开发难度高

                // 拿到userId所有的班级id classId

                Integer classId = classTeacherStudentDao.findClassIdByUserId(userId);
                attendanceDao.addAttendance(userId,classId,delayDate);
            }
        }catch (Exception exception){
            return 0;
        }
        return 1;
    }


    @Override
    public ClassBean findTeacherAndStudentListByClassIdAndStudentName(Integer classId, String studentName) {
        ClassBean classBean = findTeacherAndStudentListByClassId(classId);

        List<StudentBean> rs = new ArrayList<>();
        List<StudentBean> studentBeanList = classBean.getStudentBeanList();
        for(StudentBean studentBean:studentBeanList){
            if(studentName.equals(studentBean.getStudentName())){
                rs.add(studentBean);
            }
        }
        classBean.setStudentBeanList(rs);
        return classBean;
    }

    @Override
    public ClassBean findTeacherAndStudentListByClassId(Integer classId) {
        DaoFactory<ClassDao> daoFactory = new DaoFactory();
        classDao =daoFactory.getDao(ClassDao.class);

        // 根据班级ID组装最终数据给Controller
        ClassBean  rs = new ClassBean();
        if(null == classId){
            return rs;
        }


        // 1 查班级
        ClassBean classBean= classDao.findClassByClassId(classId);
        String className = classBean.getClassName();
        rs.setClassId(classId);
        rs.setClassName(className);

        // 2 查老师
        TeacherBean teacherBean = classDao.findTeacherBeanByClassId(classId);
        rs.setTeacherBean(teacherBean);

        // 3 查学生
        List<StudentBean> studentBeanList = classDao.findStudentBeanListByClassId(classId);
        rs.setStudentBeanList(studentBeanList);


        return rs;
    }




    @Override
    public List<ClassEntity> findAllClass() {
        DaoFactory<ClassDao> daoFactory = new DaoFactory();
        classDao =daoFactory.getDao(ClassDao.class);
        // ClassDao classDao= DaoUtil.getClassDao();
        return classDao.findAllClass();
    }

    @Override
    public ClassEntity findClassById(Integer id) {
        DaoFactory<ClassDao> daoFactory = new DaoFactory();
        classDao =daoFactory.getDao(ClassDao.class);
        return classDao.findById(id);
    }

}
